//! @Author: DengLibin
//! @Date: Create in 2024-07-17 15:37:26
//! @Description:



use crate::{
    dao::{self, PageData},
    sys::global::GlobalResult,
};

use my_trait::{MustHaveFieldType, MustHaveTableName, Struct2Map};
use serde::Serialize;
use serde_json::Value;
use sqlx::{mysql::MySqlRow, FromRow, MySql, Pool};

pub mod user_service;
pub mod article_service;

/// @Author: DengLibin
/// @Date: Create in 2024-07-18 16:05:59
/// @Description: 根据id查询
pub async fn get_by_id<E>(id: i64, pool: &Pool<MySql>) -> GlobalResult<Option<E>>
where
    E: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + MustHaveTableName,
{
    dao::mysql::mysql_dao::get_by_id::<E>(id, pool).await
}

/// @Author: DengLibin
/// @Date: Create in 2024-07-17 15:39:38
/// @Description: 分页获取
pub async fn query_page<E>(page: i32, size: i32, pool: &Pool<MySql>) -> GlobalResult<PageData<E>>
where
    E: Serialize + Send + Unpin + for<'r> FromRow<'r, MySqlRow> + MustHaveTableName,
{
    dao::mysql::mysql_dao::query_page(page, size, pool).await
}

/// @Author: DengLibin
/// @Date: Create in 2024-07-18 16:43:20
/// @Description: 根据id删除
pub async fn delete_by_id<E>(id: i64, pool: &Pool<MySql>) -> GlobalResult<()>
where
    E: MustHaveTableName,
{
    dao::mysql::mysql_dao::delete_by_id::<E>(id, pool).await
}

/// @Author: DengLibin
/// @Date: Create in 2024-07-19 16:36:46
/// @Description: 更新
pub async fn update_one<E>(id: Value, e: E, pool: &Pool<MySql>) -> GlobalResult<()>
where
    E: MustHaveTableName + MustHaveFieldType + Struct2Map,
{
    dao::mysql::mysql_dao::update_one::<E>(id, e, pool).await
}